package com.xiaozhengkeji.mc.plugins.cdkey.base;

import com.google.gson.Gson;
import com.xiaozhengkeji.mc.plugins.cdkey.config.FileConfig;
import com.xiaozhengkeji.mc.plugins.cdkey.entitys.CDKeyEntity;
import org.bukkit.configuration.file.YamlConfiguration;
import org.sqlite.SQLiteConfig;
import org.sqlite.SQLiteDataSource;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class SQLiteFactory {


    //存储CDK
    public int addCDKey(CDKeyEntity cdKeyEntity) {
        YamlConfiguration BaseYml = YamlConfiguration.loadConfiguration(new File(FileConfig.Plugin_File_Base));
        BaseYml.set(cdKeyEntity.cdkey, new Gson().toJson(cdKeyEntity));
        try {
            BaseYml.save(FileConfig.Plugin_File_Base);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 1;
    }

    //查询CDK 更具备注
    public List<CDKeyEntity> getCDKeyByPostscript(String postscript) {
        YamlConfiguration BaseYml = YamlConfiguration.loadConfiguration(new File(FileConfig.Plugin_File_Base));
        List<CDKeyEntity> cdKeyEntityList = new LinkedList<>();
        for (String key : BaseYml.getKeys(false)) {
            CDKeyEntity cdKeyEntity = new Gson().fromJson(BaseYml.getString(key), CDKeyEntity.class);
            if (cdKeyEntity.b_postscript.equalsIgnoreCase(postscript)) {
                cdKeyEntityList.add(cdKeyEntity);
            }
        }
        return cdKeyEntityList;
    }

    //查询CDK 更具创建者
    public List<CDKeyEntity> getCDKeyByCreator(String creator) {
        YamlConfiguration BaseYml = YamlConfiguration.loadConfiguration(new File(FileConfig.Plugin_File_Base));
        List<CDKeyEntity> cdKeyEntityList = new LinkedList<>();
        for (String key : BaseYml.getKeys(false)) {
            CDKeyEntity cdKeyEntity = new Gson().fromJson(BaseYml.getString(key), CDKeyEntity.class);
            if (cdKeyEntity.up_creator.equalsIgnoreCase(creator)) {
                cdKeyEntityList.add(cdKeyEntity);
            }
        }
        return cdKeyEntityList;
    }

    //查询CDK 更具CDK
    public CDKeyEntity getCDKeyByCDKey(String cdkey) {
        YamlConfiguration BaseYml = YamlConfiguration.loadConfiguration(new File(FileConfig.Plugin_File_Base));
        List<CDKeyEntity> cdKeyEntityList = new LinkedList<>();
        for (String key : BaseYml.getKeys(false)) {
            return new Gson().fromJson(BaseYml.getString(key), CDKeyEntity.class);
        }
        return null;
    }


    //删除CDK
    public void delCDKey(String cdkey) {
        YamlConfiguration BaseYml = YamlConfiguration.loadConfiguration(new File(FileConfig.Plugin_File_Base));
        BaseYml.set(cdkey, null);
        try {
            BaseYml.save(FileConfig.Plugin_File_Base);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //修改CDK
    public void upCDKey(CDKeyEntity cdKeyEntity) {
        YamlConfiguration BaseYml = YamlConfiguration.loadConfiguration(new File(FileConfig.Plugin_File_Base));
        BaseYml.set(cdKeyEntity.cdkey, new Gson().toJson(cdKeyEntity));
        try {
            BaseYml.save(FileConfig.Plugin_File_Base);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
